package com.wjbgn.electronicnian.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianWordsDO;
import com.wjbgn.electronicnian.dto.NianWordsDTO;
import com.wjbgn.electronicnian.service.NianWordsService;
import com.wjbgn.electronicnian.convert.NianWordsDoConvert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * Description: 
 * Create Date: 2022-01-20T11:06:12.907434600
 * @author weirx
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/nianwords")
@AllArgsConstructor
public class NianWordsController {

    /**
     * NianWordsService
     */
    private NianWordsService nianWordsService;

    /**
     * 分页列表
     * @param nianWordsDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianWordsDTO nianWordsDTO) {
        QueryWrapper<NianWordsDO> queryWrapper = new QueryWrapper<>(NianWordsDoConvert.dtoToDo(nianWordsDTO));
        Page<NianWordsDO> page = new Page<>(nianWordsDTO.getPage(), nianWordsDTO.getLimit());
        Page<NianWordsDO> pageList = nianWordsService.page(page, queryWrapper);
        return Result.success(NianWordsDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     * @param nianWordsDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(NianWordsDTO nianWordsDTO) {
        QueryWrapper<NianWordsDO> queryWrapper = new QueryWrapper<>(NianWordsDoConvert.dtoToDo(nianWordsDTO));
        List<NianWordsDO> nianWordsList = nianWordsService.list(queryWrapper);
        return Result.success(NianWordsDoConvert.listConvert(nianWordsList));
    }

    /**
     * 随机获取
     * @param nianId
     * @return
     */
    @ApiOperation(value = "随机获取")
    @GetMapping("/info/getRandom")
    public Result info(@RequestParam("nianId") Long nianId) {
        List<NianWordsDO> list = nianWordsService.list(new QueryWrapper<>(new NianWordsDO(nianId)));
        if (list.size() == 0){
            return Result.failed("我还没学会祝福的话呢，主人您教我一句吧！教的越多，越有机会获得饺子");
        }
        int i = RandomUtil.randomInt(list.size());
        NianWordsDO nianWords = list.get(i);
        return Result.success(nianWords);
    }

    /**
     * 新增
     * @param nianWordsDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianWordsDTO nianWordsDTO) {
        return nianWordsService.learnWords(NianWordsDoConvert.dtoToDo(nianWordsDTO));

    }

    /**
     * 更新
     * @param nianWordsDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianWordsDTO nianWordsDTO) {
        boolean flag = nianWordsService.updateById(NianWordsDoConvert.dtoToDo(nianWordsDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }
}